#include "GameConfig.h"
#include "cocos2d.h"
#include "tinyxml2\tinyxml2.h"
GameConfig * GameConfig::me = NULL;
GameConfig & GameConfig::getInstance()
{
	if (!me) me = new GameConfig();
	return *me;
}
GameConfig::GameConfig()
{
	init();
}

void GameConfig::init()
{
	std::string content = cocos2d::FileUtils::getInstance()->getStringFromFile("GameInfo.xml");
	tinyxml2::XMLDocument doc;
	doc.Parse(content.c_str());
	tinyxml2::XMLElement * root = doc.RootElement();
	if (root)
	{
		tinyxml2::XMLElement * imagesElement = root->FirstChildElement("images");
		if (imagesElement)
		{
			tinyxml2::XMLElement * imageElement = imagesElement->FirstChildElement("image");
			while (imageElement)
			{
				const char *imageName = imageElement->Attribute("name");
				if (imageName)
					buttonNames.push_back(imageName);
				imageElement = imageElement->NextSiblingElement("image");
			}
		}
	}
}

int GameConfig::getLevelByName(const char *name)
{
	for (int i = 0; i < buttonNames.size(); ++i)
	{
		if (buttonNames[i] == std::string(name))
		{
			return i + 1;
		}
	}
	return 0;
}